package keating;

import com.google.gdata.client.douban.DoubanService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.TextContent;
import com.google.gdata.data.douban.MiniblogEntry;
import com.google.gdata.data.douban.MiniblogFeed;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.oauth.OAuthException;

/**
 *
 * @author Keating wang
 */
public enum Business {
    
    Business;

    /**
     * service
     */
    private DoubanService service = null;
    /**
     * 是否校验通过
     */
    private boolean authorize = false;
    
    private Business() {
//         String apiKey = "0b7f3665301a646a227da32f904f2d3e";
//         String secret = "748352eb74706825";
//         service = new DoubanService("douban-jar", apiKey, secret);
        String apiKey = "059ef56f6b705e1210dce04e42511a36";
        String secret = "006ba4a489916c13";
        try {
            service = new LoginInfo().read();
        } catch (IOException ex) {
            ex.printStackTrace();
            service = new DoubanService("subApplication", apiKey, secret);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Business.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * 获取授权字符串
     */
    public String authorizeStr() {
        return service.getAuthorizationUrl(null);
    }
    
    /**
     * 授权
     */
    public void authorize() throws OAuthException {
//        if(this.authorize)
//            return;
        service.getAccessToken();
        this.authorize = true;
        System.out.println("--------------授权成功-----------------");
        try {
            new LoginInfo().write(this.service);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Business.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Business.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * 查询前10条微博
     */
    public List<MiniblogEntry> query() {
        return query(10);
    }
    
    /**
     * 查询微博 
     * @param  qty  微博条数,从最近一条开始计数
     */
    public List<MiniblogEntry> query(int qty) {
        if(!authorize)
            return new ArrayList<MiniblogEntry>();
        List<MiniblogEntry> blogs = new ArrayList<MiniblogEntry>();
        try {
            String userId = service.getAuthorizedUser().getUid();
            // TODO 保存授权用户的ID
            
            System.out.println("user:"+userId+" " + qty);
            MiniblogFeed mf = service.getUserMiniblogs(userId, 1, qty);
            for (MiniblogEntry me : mf.getEntries()) {
                // 若是微博
                if (me.getAttributes().size() > 0 && me.getAttributes().get(0).getName().equals("comments_count")) {
                    blogs.add(me);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(Business.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("+++++++++++++=blogs:"+blogs.size());
        return blogs;
    }
    
    /**
     * 发送微博
     */
    public void send(String text) {
        if(!authorize)
            return;
        try {
            service.createSaying(new PlainTextConstruct(text));
        } catch (Exception ex) {
            Logger.getLogger(Business.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * 删除微博
     */
    public void delete(MiniblogEntry me) {
        if(!authorize)
            return;
        try {
            service.deleteMiniblog(me);
        } catch (Exception ex) {
            Logger.getLogger(Business.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
